﻿// **********************************************************************
//
// Author: KingSley
// Email:3268374537@qq.com
// Created:	2018/8/1   11:10
// Introduce: 对过度场景进行控制
//
// **********************************************************************
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace KingSley
{
    public class LoadingSceneController : MonoSingleton<LoadingSceneController>
    {
        AsyncOperation async = new AsyncOperation();

        public Image m_pProgress;
        private int progress = 0;

        void Start()
        {
            StartCoroutine(LoadScenes());
        }

        IEnumerator LoadScenes()
        {
            int nDisPlayProgress = 0;
            async = SceneManager.LoadSceneAsync(GlobelScene.SceneName);
            async.allowSceneActivation = false;
            while (async.progress < 0.9f)
            {
                progress = (int)async.progress * 100;
                while (nDisPlayProgress < progress)
                {
                    ++nDisPlayProgress;
                    m_pProgress.fillAmount = (float)nDisPlayProgress / 100;
                    yield return new WaitForEndOfFrame();
                }
                yield return null;
            }
            progress = 100;
            while (nDisPlayProgress < progress)
            {
                ++nDisPlayProgress;
                m_pProgress.fillAmount = (float)nDisPlayProgress / 100;
                yield return new WaitForEndOfFrame();
            }
            async.allowSceneActivation = true;
        }
    }
}